package main.java.FSMDE.infrastructure.visual;


import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import main.java.FSMDE.infrastructure.IProjectContext;
import main.java.FSMDE.models.Diagram;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.Transition;


public class DiagramTreeNode extends DefaultMutableTreeNode {

	private static final long serialVersionUID = 1L;
	private IProjectContext projectContext;
	private Diagram diagram;
	private DefaultMutableTreeNode transitionsNode;
	private DefaultMutableTreeNode interfacesNode;
	private DefaultMutableTreeNode subdiagramsNode;
	private static String interfacesNodeName = "Interfaces";
	private static String transitionsNodeName = "Transitions";
	private static String subdiagramsNodeName = "Sub-Diagrams";

	public DiagramTreeNode(Diagram diagram, IProjectContext projectContext){
		super(diagram);
		this.projectContext = projectContext;
		this.diagram = diagram;
		this.addChildFolders();
		this.populateInterfaces();
		this.populateTransitions();
		this.populateSubDiagrams();
	}

	private void populateTransitions() {
		for( Transition transition : diagram.getTransitions() ){
			transitionsNode.add(new DefaultMutableTreeNode(transition));
		}
	}

	private void populateInterfaces() {
		for( Interface inteface : diagram.getInterfaces() ){
			DefaultMutableTreeNode interfaceNode = new DefaultMutableTreeNode(inteface);
			interfacesNode.add(interfaceNode);
		}		
	}

	private void populateSubDiagrams(){
		for(Diagram subDiagram : diagram.getSubDiagrams()){
			this.subdiagramsNode.add( new DiagramTreeNode(subDiagram, this.projectContext));
		}
	}
	private void addChildFolders() {
		this.interfacesNode = new DefaultMutableTreeNode(interfacesNodeName);
		this.transitionsNode = new DefaultMutableTreeNode(transitionsNodeName);
		this.subdiagramsNode = new DefaultMutableTreeNode(subdiagramsNodeName);
		this.add(this.interfacesNode);
		this.add(this.transitionsNode);
		this.add(this.subdiagramsNode);
	}
	
	public Diagram getDiagram(){
		return diagram;
	}
	
	public void addInterface(Interface interface0, DefaultTreeModel tree){
		DefaultMutableTreeNode interfaceNode = new DefaultMutableTreeNode(interface0);
		this.interfacesNode.add(interfaceNode);
		int index = this.interfacesNode.getChildCount() - 1;
		tree.nodesWereInserted(this.interfacesNode, new int[]{index});
	}
	
	public void addTransition(Transition transition, DefaultTreeModel tree){
		this.transitionsNode.add(new DefaultMutableTreeNode(transition));
		int index = this.transitionsNode.getChildCount() - 1;
		tree.nodesWereInserted(this.transitionsNode, new int[]{index});
	}
	
	public DiagramTreeNode addSubdiagram(Diagram diagram, DefaultTreeModel tree){
		DiagramTreeNode diagramNode = new DiagramTreeNode(diagram, this.projectContext);
		this.subdiagramsNode.add(diagramNode);
		int index = this.subdiagramsNode.getChildCount() - 1;
		tree.nodesWereInserted(this.subdiagramsNode, new int[]{index});
		return diagramNode;
	}
}
